<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="coca" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <!-- 添加一级部门 -->
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- tree树形控件 -->
        <el-tree :data="list" :props="{children: 'children',label: 'name'}">
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%">
              <el-col :span="20">
                <svg-icon icon-class="coca" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children && data.children.length === 0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog
        title="新增子部门"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="hClose"
      >
        <deptDialog
          :id="curId"
          ref="myDept"
          :is-edit="isEdit"
          :origin-list="originList"
          @success="hSuccess"
          @close="showDialog = false"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartments } from '@/api/departments'
import { tranListToTreeList } from '@/utils'
import deptDialog from './deptDialog.vue'
export default {
  components: {
    deptDialog
  },
  data() {
    return {
      list: [],
      showDialog: false,
      curId: '',
      isEdit: false,
      originList: []
    }
  },
  created() {
    // 加载部门列表
    this.loadDepartments()
  },
  methods: {
    hSuccess() {
      // 关闭弹层
      this.showDialog = false
      // 刷新页面
      this.loadDepartments()
    },
    hEdit(id) {
      this.isEdit = true
      this.showDialog = true
      this.curId = id
      // 解决数据不更新的问题
      this.$nextTick(() => {
        this.$refs.myDept.loadDepartmentsDetail()
      })
    },
    hAdd(id) {
      this.isEdit = false
      this.showDialog = true
      this.curId = id
    },
    // 删除  当该部门有子级时, 不提供删除服务  在删除服务加一个v-if判断
    hDel(id) {
      this.$confirm('您确定要删除该部门吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delDepartments(id)
        console.log(res)
        // 刷新页面
        this.loadDepartments()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 子组件点击取消按钮,通过子传父将事件传过来, 关闭弹层
    hClose() {
      console.log('弹层关闭')
      this.$refs.myDept.resetForm()
    },
    // 获取部门列表
    async loadDepartments() {
      try {
        const res = await getDepartments()
        // console.log(res.data.data.depts, '部门列表')
        this.originList = res.data.data.depts.map(item => {
          const { name, code, id, pid } = item
          return { name, code, id, pid }
        })
        console.log(this.originList, 'originList')
        this.list = tranListToTreeList(res.data.data.depts)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
<style>

</style>
